import { GameRoleCtrl } from './../../game/GameCtrl';
import GameRoleModel from "../../game/model/GameRoleModel";
import BaseWidget from "../BaseWidget";

const {ccclass, property, menu} = cc._decorator;

@ccclass
@menu("Widget/Game/GameRoleWidget")
export default class GameRoleWidget extends BaseWidget {
    @property(cc.Animation)
    anim_role: cc.Animation = null;
    @property(cc.Node)
    node_image: cc.Node = null;

    @property(cc.Animation)
    anim_levelUp: cc.Animation = null;

    @property(cc.Label)
    lb_attack: cc.Label = null;
    
    
    @property(cc.String)
    str_role_idle_anim_left: string = "";
    @property(cc.String)
    str_role_idle_anim_right: string = "";
    @property(cc.String)
    str_role_idle_anim_up: string = "";
    @property(cc.String)
    str_role_idle_anim_down: string = "";

    protected onLoad(): void {
        this.anim_levelUp.node.active = false;
        this.node.active = false;
    }

    public init(pos: cc.Vec2){
        this.node.setPosition(pos);
        this.node.active = true;
        this.node_image.opacity = 255;
        this.updateProp();
    }

    public exit(){
        this.node.active = false;
    }

    public runAnim(dir: cc.Vec2){
        let animName = "";
        if(dir.fuzzyEquals(cc.Vec2.UP, 0.1)){
            animName = this.str_role_idle_anim_up;
        }else if(dir.fuzzyEquals(cc.Vec2.RIGHT, 0.1)){
            animName = this.str_role_idle_anim_right;
        }else if(dir.fuzzyEquals(cc.Vec2.UP.neg(), 0.1)){
            animName = this.str_role_idle_anim_down;
        }else if(dir.fuzzyEquals(cc.Vec2.RIGHT.neg(), 0.1)){
            animName = this.str_role_idle_anim_left;
        }
        
        if(animName == ""){
            this.anim_role.stop();
        }else if(this.anim_role.currentClip && this.anim_role.currentClip.name == animName) {
            this.anim_role.resume();
        }else{
            this.anim_role.play(animName);
        }
    }

    public runStop(){
        this.anim_role.stop();
    }

    public runMove(pos: cc.Vec2, dir: cc.Vec2){
        this.runAnim(dir);
        return new Promise((resolve, reject)=>{
            cc.tween(this.node)
            .to(0.2, {x: pos.x, y: pos.y})
            .call(()=>{
                resolve(null);
            })
            .start();
        });
    }

    public runAttack(dir: cc.Vec2){
        this.runAnim(dir);
        return new Promise((resolve, reject)=>{
            cc.tween(this.node_image)
            .by(0.25, {x: dir.x * 50, y: dir.y * 50}, {"easing": "quadOut"})
            .call(()=>{
                resolve(null);
            })
            .to(0.2, {x: 0, y: 0}, {"easing": "quadIn"})
            .start();
        });
    }

    public runDead(){
        return new Promise((resolve, reject)=>{
            cc.tween(this.node_image)
            .to(0.2, {opacity: 0}, {"easing": "quadOut"})
            .call(resolve)
            .start();
        });
    }

    public updateProp(isLevelUp = false){
        let data = GameRoleModel.getInstance().prop;

        this.lb_attack.string = data.atk + "";

        if(isLevelUp){
            this.anim_levelUp.node.active = true;
            this.anim_levelUp.once(cc.Animation.EventType.FINISHED, ()=>{
                this.anim_levelUp.node.active = false;
            });
            this.anim_levelUp.play();
        }
    }
}